<?php

namespace app\api\controller;

use app\common\controller\Admin as AdminCommon;
use app\common\library\Ems;
use app\common\library\Code;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use think\Db;

/**
 * 会员接口
 */
class Admin extends AdminCommon
{
    protected $noNeedLogin = ['login', 'register', "registCode", "getGameLists", "getGameDetail", "test"];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 会员中心
     */
    public function index()
    {
        $this->success('', ['welcome' => $this->admin->nickname]);
    }

    /**
     * 管理员登录
     *
     * @ApiMethod (POST)
     * @param string $password 密码
     */
    public function login()
    {
        $password = $this->request->post('password');
        if (!$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->admin->login($password);

        if ($ret) {
            $data = $this->admin->getAdmininfo();
            $this->success(__('登录成功'), $data);
        } else {
            $this->error($this->admin->getError());
        }
    }
    /**
     * 注册会员
     *
     * @ApiMethod (POST)
     * @param string $username 用户名
     * @param string $password 密码
     * @param string $email    邮箱
     * @param string $mobile   手机号
     * @param string $code     验证码
     */
    public function register()
    {
        $username = $this->request->post('username');
        $password = $this->request->post('password');
        $email = $this->request->post('email');
        $mobile = $this->request->post('mobile');
        $code = $this->request->post('code');
        if (!$username || !$password) {
            $this->error(__('Invalid parameters'));
        }
        if ($email && !Validate::is($email, "email")) {
            $this->error(__('Email is incorrect'));
        }
        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $ret = Sms::check($mobile, $code, 'register');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        $ret = $this->admin->register($username, $password, $email, $mobile, []);
        if ($ret) {
            $data = ['userinfo' => $this->admin->getUserinfo()];
            $this->success(__('Sign up successful'), $data);
        } else {
            $this->error($this->admin->getError());
        }
    }

    /**
     * 填写注册码，登录会员/注册会员
     *
     * @ApiMethod (POST)
     */
    public function registCode()
    {
        //注册码
        $regist_code = $this->request->post('regist_code');
        $mobile_id = $this->request->post('mobile_id');
       
        if (!$regist_code || !$mobile_id) {
            $this->error(__('Invalid parameters'));
        }
       
        $ret = Code::check($regist_code);
        if (!$ret) {
            $this->error("注册码错误，登录失败");
        }

        //注册码正确，通过设备id查询是否已经是用户了，是用户则登录，不是则注册
        $userRet = $this->admin->getUserInfo_mobileId($mobile_id);

        //用户未注册，先注册，生成用户信息，并登录
        if($userRet == 2){
            $createRet = $this->admin->registCode($mobile_id, "123123");
            if($createRet){
                $data = ['userinfo' => $this->admin->getUserinfo()];
                $this->success(__('Sign up successful'), $data);
            }else{
                $this->error("注册失败，请联系管理员");
            }
        }

        if($userRet == 3){
            $this->error("设备被冻结");
        }
        
        //直接登录
        $loginRet = $this->admin->loginMobileId($mobile_id, "123123");

        if ($loginRet) {
            $data = ['userinfo' => $this->admin->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->admin->getError());
        }
    }

    /**
     * 退出登录
     * @ApiMethod (POST)
     */
    public function logout()
    {
        if (!$this->request->isPost()) {
            $this->error(__('Invalid parameters'));
        }
        $this->admin->logout();
        $this->success(__('Logout successful'));
    }

    //管理端 主料/辅料列表
    public function getGameListsAdmin()
    {
        //地区
        $area_type = $this->request->post("area_type", 0);
        //2主料 1辅料
        $type = $this->request->post("type", 1);
        //分页信息
        $page = $this->request->post("page", 1);
        $limit = $this->request->post("limit", 10);

        $lists = Db::name("game")
        ->field("id, stage_name, number, zodiac, release")
        ->where("area_type = " . $area_type)
        ->where("type = " . $type)
        ->page($page, $limit)
        ->order("releasetime desc")
        ->select();

        $this->success("操作成功", $lists);
    }

    //查询某一期的详情数据
    public function getGameListsDetailAdmin()
    {
        $id = $this->request->post("id", false);

        if(!$id){
            $this->error("参数错误");
        }

        $detail = Db::name("game")
        ->where("id = " . $id)
        ->find();

        if(!$detail){
            $this->error("参数错误，无数据");
        }

        $detail["number"] = explode(",", $detail["number"]);
        $detail["zodiac"] = explode(",", $detail["zodiac"]);
        $detail["one_zodiac_one_number"] = json_decode($detail["one_zodiac_one_number"], true);
        $detail["three_zodiac_therr_number"] = json_decode($detail["three_zodiac_therr_number"], true);
        $detail["six_zodiac"] = json_decode($detail["six_zodiac"], true);
        $detail["single_double"] = json_decode($detail["single_double"], true);
        $detail["first_number"] = json_decode($detail["first_number"], true);
        $detail["last_number"] = json_decode($detail["last_number"], true);
        $detail["color"] = json_decode($detail["color"], true);
        $detail["one_zodiac"] = json_decode($detail["one_zodiac"], true);
        $detail["six_number"] = json_decode($detail["six_number"], true);
        $detail["one_number"] = json_decode($detail["one_number"], true);
        $detail["releasetime"] = $detail["releasetime"] ? Date("Y-m-d", $detail["releasetime"]) : "";

        //查询当前所有活动名称
        $game_name_lists = Db::name("game_name")
        ->field("id, title, name")
        ->where("area_type = " . $detail["area_type"])
        ->select();

        $detail["game_name_lists"] = $game_name_lists;

        $this->success("操作成功", $detail);
    }

    //添加一期活动
    public function addGame()
    {
        $release = $this->request->post("release", 0);
        $releasetime = $this->request->post("releasetime", "");
        $area_type = $this->request->post("area_type", 0);
        $type = $this->request->post("type", 0);
        $stage_no = $this->request->post("stage_no", 0);
        $number = $this->request->post("number", "");
        // $number_lock = $this->request->post("number_lock", 0);
        $zodiac = $this->request->post("zodiac", "");
        // $zodiac_lock = $this->request->post("zodiac_lock", 0);

        if($releasetime == ""){
            $this->error("缺少发布日期");
        }

        if($area_type == 0){
            $this->error("缺少发布地区");
        }

        if($type == 0){
            $this->error("缺少发布类型");
        }

        $releasetime_start = strtotime($releasetime . " 00:00:00");
        //页面只设置日期，发布时分秒按照当天的 23:59:59 算
        $releasetime = strtotime($releasetime . " 23:59:59");

        //以下参数都是json数组 以及锁定状态
        $one_zodiac_one_number = $this->request->post("one_zodiac_one_number", "");
        $one_zodiac_one_number_lock = $this->request->post("one_zodiac_one_number_lock", 0);

        $three_zodiac_therr_number = $this->request->post("three_zodiac_therr_number", "");
        $three_zodiac_therr_number_lock = $this->request->post("three_zodiac_therr_number_lock", 0);

        $six_zodiac = $this->request->post("six_zodiac", "");
        $six_zodiac_lock = $this->request->post("six_zodiac_lock", 0);

        $single_double = $this->request->post("single_double", "");
        $single_double_lock = $this->request->post("single_double_lock", 0);

        $first_number = $this->request->post("first_number", "");
        $first_number_lock = $this->request->post("first_number_lock", 0);

        $last_number = $this->request->post("last_number", "");
        $last_number_lock = $this->request->post("last_number_lock", 0);

        $color = $this->request->post("color", "");
        $color_lock = $this->request->post("color_lock", 0);

        $one_zodiac = $this->request->post("one_zodiac", "");
        $one_zodiac_lock = $this->request->post("one_zodiac_lock", 0);

        $six_number = $this->request->post("six_number", "");
        $six_number_lock = $this->request->post("six_number_lock", 0);

        $one_number = $this->request->post("one_number", "");
        $one_number_lock = $this->request->post("one_number_lock", 0);

        //数据处理
        $one_zodiac_one_number = htmlspecialchars_decode($one_zodiac_one_number);
        $three_zodiac_therr_number = htmlspecialchars_decode($three_zodiac_therr_number);
        $six_zodiac = htmlspecialchars_decode($six_zodiac);
        $single_double = htmlspecialchars_decode($single_double);
        $first_number = htmlspecialchars_decode($first_number);
        $last_number = htmlspecialchars_decode($last_number);
        $color = htmlspecialchars_decode($color);
        $one_zodiac = htmlspecialchars_decode($one_zodiac);
        $six_number = htmlspecialchars_decode($six_number);
        $one_number = htmlspecialchars_decode($one_number);

        // $one_zodiac_one_number = str_replace("&quot;", '"', $one_zodiac_one_number);
        // $three_zodiac_therr_number = str_replace("&quot;", '"', $three_zodiac_therr_number);
        // $six_zodiac = str_replace("&quot;", '"', $six_zodiac);
        // $single_double = str_replace("&quot;", '"', $single_double);
        // $first_number = str_replace("&quot;", '"', $first_number);
        // $last_number = str_replace("&quot;", '"', $last_number);
        // $color = str_replace("&quot;", '"', $color);
        // $one_zodiac = str_replace("&quot;", '"', $one_zodiac);
        // $six_number = str_replace("&quot;", '"', $six_number);
        // $one_number = str_replace("&quot;", '"', $one_number);

        //补全3位数
        $var=sprintf("%03d", $stage_no);
        $stage_name = "第" . $var . "期";
        $createtime = time();

        //查询期数是否重复
        $info = Db::name("game")
        ->where("stage_no = " . $stage_no)
        ->where("area_type = " . $area_type)
        ->where("type = " . $type)
        ->find();
        if($info){
            $this->error("期数已存在，请修改期数");
        }

        $data = [];

        $data["release"] = $release;
        $data["releasetime"] = $releasetime;
        $data["area_type"] = $area_type;
        $data["type"] = $type;
        $data["stage_no"] = $stage_no;
        $data["stage_name"] = $stage_name;
        $data["number"] = $number;
        // $data["number_lock"] = $number_lock;
        $data["zodiac"] = $zodiac;
        // $data["zodiac_lock"] = $zodiac_lock;

        $data["one_zodiac_one_number"] = $one_zodiac_one_number;
        $data["one_zodiac_one_number_lock"] = $one_zodiac_one_number_lock;
        $data["three_zodiac_therr_number"] = $three_zodiac_therr_number;
        $data["three_zodiac_therr_number_lock"] = $three_zodiac_therr_number_lock;
        $data["six_zodiac"] = $six_zodiac;
        $data["six_zodiac_lock"] = $six_zodiac_lock;
        $data["single_double"] = $single_double;
        $data["single_double_lock"] = $single_double_lock;
        $data["first_number"] = $first_number;
        $data["first_number_lock"] = $first_number_lock;
        $data["last_number"] = $last_number;
        $data["last_number_lock"] = $last_number_lock;
        $data["color"] = $color;
        $data["color_lock"] = $color_lock;
        $data["one_zodiac"] = $one_zodiac;
        $data["one_zodiac_lock"] = $one_zodiac_lock;
        $data["six_number"] = $six_number;
        $data["six_number_lock"] = $six_number_lock;
        $data["one_number"] = $one_number;
        $data["one_number_lock"] = $one_number_lock;
        $data["createtime"] = $createtime;

        Db::name("game")
        ->insert($data);
        $res = Db::name("game")->getLastInsID();

        //查询发布日期注册的用户列表，给用户进行绑定主料数据
        if($type == 2 && $release == 1){
            $user_lists = Db::name("user")
            ->field("id, createtime")
            ->where("createtime >= " . $releasetime_start)
            ->where("createtime <= " . $releasetime)
            ->select();
            if($user_lists){
                // $user_lists = $user_lists->toArray();
                foreach($user_lists as &$value){
                    //判断用户是否在这一天同一个地区已经有这一期的数据了
                    $game_user_info = Db::name("game_user")
                    ->where("uid = " . $value["id"])
                    ->where("game_id = " . $res)
                    ->find();
                    if(!$game_user_info){ //没有数据，才需要绑定
                        $data["uid"] = $value["id"];
                        $data["game_id"] = $res;
                        Db::name("game_user")->insert($data);
                    }
                }
            }
        }

        $this->success("添加成功", ["id" => $res]);
    }

    //修改一期活动
    public function editGame()
    {
        $id = $this->request->post("id", 0);
        $release = $this->request->post("release", 0);
        $releasetime = $this->request->post("releasetime", "");
        $area_type = $this->request->post("area_type", 0);
        $type = $this->request->post("type", 0);
        $stage_no = $this->request->post("stage_no", 0);
        $number = $this->request->post("number", "");
        // $number_lock = $this->request->post("number_lock", 0);
        $zodiac = $this->request->post("zodiac", "");
        // $zodiac_lock = $this->request->post("zodiac_lock", 0);

        if($releasetime == ""){
            $this->error("缺少发布日期");
        }

        if($area_type == 0){
            $this->error("缺少发布地区");
        }

        if($type == 0){
            $this->error("缺少发布类型");
        }

        //查询这一期活动详情
        $detail_info = Db::name("game")
        ->where("stage_no = " . $stage_no)
        ->where("area_type = " . $area_type)
        ->where("type = " . $type)
        ->where("id = " . $id)
        ->find();

        $releasetime_start = strtotime($releasetime . " 00:00:00");
        //页面只设置日期，发布时分秒按照当天的 23:59:59 算
        $releasetime = strtotime($releasetime . " 23:59:59");

        //以下参数都是json数组 以及锁定状态
        $one_zodiac_one_number = $this->request->post("one_zodiac_one_number", "");
        $one_zodiac_one_number_lock = $this->request->post("one_zodiac_one_number_lock", 0);
        if($one_zodiac_one_number_lock == 0 && $detail_info["one_zodiac_one_number_lock"] == 1){
            $this->error("一肖一码已锁定，请勿解锁");
        }

        $three_zodiac_therr_number = $this->request->post("three_zodiac_therr_number", "");
        $three_zodiac_therr_number_lock = $this->request->post("three_zodiac_therr_number_lock", 0);
        if($three_zodiac_therr_number_lock == 0 && $detail_info["three_zodiac_therr_number_lock"] == 1){
            $this->error("三肖三码已锁定，请勿解锁");
        }

        $six_zodiac = $this->request->post("six_zodiac", "");
        $six_zodiac_lock = $this->request->post("six_zodiac_lock", 0);
        if($six_zodiac_lock == 0 && $detail_info["six_zodiac_lock"] == 1){
            $this->error("皇冠六肖已锁定，请勿解锁");
        }

        $single_double = $this->request->post("single_double", "");
        $single_double_lock = $this->request->post("single_double_lock", 0);
        if($single_double_lock == 0 && $detail_info["single_double_lock"] == 1){
            $this->error("无敌单双已锁定，请勿解锁");
        }

        $first_number = $this->request->post("first_number", "");
        $first_number_lock = $this->request->post("first_number_lock", 0);
        if($first_number_lock == 0 && $detail_info["first_number_lock"] == 1){
            $this->error("财运二头已锁定，请勿解锁");
        }

        $last_number = $this->request->post("last_number", "");
        $last_number_lock = $this->request->post("last_number_lock", 0);
        if($last_number_lock == 0 && $detail_info["last_number_lock"] == 1){
            $this->error("内部五尾已锁定，请勿解锁");
        }

        $color = $this->request->post("color", "");
        $color_lock = $this->request->post("color_lock", 0);
        if($color_lock == 0 && $detail_info["color_lock"] == 1){
            $this->error("精准二波已锁定，请勿解锁");
        }

        $one_zodiac = $this->request->post("one_zodiac", "");
        $one_zodiac_lock = $this->request->post("one_zodiac_lock", 0);
        if($one_zodiac_lock == 0 && $detail_info["one_zodiac_lock"] == 1){
            $this->error("家野爆特已锁定，请勿解锁");
        }

        $six_number = $this->request->post("six_number", "");
        $six_number_lock = $this->request->post("six_number_lock", 0);
        if($six_number_lock == 0 && $detail_info["six_number_lock"] == 1){
            $this->error("三中三已锁定，请勿解锁");
        }

        $one_number = $this->request->post("one_number", "");
        $one_number_lock = $this->request->post("one_number_lock", 0);
        if($one_number_lock == 0 && $detail_info["one_number_lock"] == 1){
            $this->error("内部一码已锁定，请勿解锁");
        }

        //数据处理
        $one_zodiac_one_number = htmlspecialchars_decode($one_zodiac_one_number);
        $three_zodiac_therr_number = htmlspecialchars_decode($three_zodiac_therr_number);
        $six_zodiac = htmlspecialchars_decode($six_zodiac);
        $single_double = htmlspecialchars_decode($single_double);
        $first_number = htmlspecialchars_decode($first_number);
        $last_number = htmlspecialchars_decode($last_number);
        $color = htmlspecialchars_decode($color);
        $one_zodiac = htmlspecialchars_decode($one_zodiac);
        $six_number = htmlspecialchars_decode($six_number);
        $one_number = htmlspecialchars_decode($one_number);

        //补全3位数
        $var=sprintf("%03d", $stage_no);
        $stage_name = "第" . $var . "期";
        $updatetime = time();

        //查询期数是否重复
        $info = Db::name("game")
        ->where("stage_no = " . $stage_no)
        ->where("area_type = " . $area_type)
        ->where("type = " . $type)
        ->where("id <> " . $id)
        ->find();
        if($info){
            $this->error("期数已存在，请修改期数");
        }

        $data = [];

        $data["release"] = $release;//修改一期活动，发布状态自动改为 未发布
        $data["releasetime"] = $releasetime;
        $data["area_type"] = $area_type;
        $data["type"] = $type;
        $data["stage_no"] = $stage_no;
        $data["stage_name"] = $stage_name;
        $data["number"] = $number;
        // $data["number_lock"] = $number_lock;
        $data["zodiac"] = $zodiac;
        // $data["zodiac_lock"] = $zodiac_lock;

        $data["one_zodiac_one_number"] = $one_zodiac_one_number;
        $data["one_zodiac_one_number_lock"] = $one_zodiac_one_number_lock;
        $data["three_zodiac_therr_number"] = $three_zodiac_therr_number;
        $data["three_zodiac_therr_number_lock"] = $three_zodiac_therr_number_lock;
        $data["six_zodiac"] = $six_zodiac;
        $data["six_zodiac_lock"] = $six_zodiac_lock;
        $data["single_double"] = $single_double;
        $data["single_double_lock"] = $single_double_lock;
        $data["first_number"] = $first_number;
        $data["first_number_lock"] = $first_number_lock;
        $data["last_number"] = $last_number;
        $data["last_number_lock"] = $last_number_lock;
        $data["color"] = $color;
        $data["color_lock"] = $color_lock;
        $data["one_zodiac"] = $one_zodiac;
        $data["one_zodiac_lock"] = $one_zodiac_lock;
        $data["six_number"] = $six_number;
        $data["six_number_lock"] = $six_number_lock;
        $data["one_number"] = $one_number;
        $data["one_number_lock"] = $one_number_lock;
        $data["updatetime"] = $updatetime;

        Db::name("game")
        ->where("id = " . $id)
        ->update($data);

        //查询发布日期注册的用户列表，给用户进行绑定主料数据
        if($type == 2 && $release == 1){
            $user_lists = Db::name("user")
            ->field("id, createtime")
            ->where("createtime >= " . $releasetime_start)
            ->where("createtime <= " . $releasetime)
            ->select();

            if($user_lists){
                // $user_lists = $user_lists->toArray();
                foreach($user_lists as &$value){
                    //判断用户是否在这一天已经有这一期的数据了
                    $game_user_info = Db::name("game_user")
                    ->where("uid = " . $value["id"])
                    ->where("game_id = " . $id)
                    ->find();
                    if(!$game_user_info){ //没有数据，才需要绑定
                        $data["uid"] = $value["id"];
                        $data["game_id"] = $id;
                        Db::name("game_user")->insert($data);
                    }else{
                        Db::name("game_user")
                        ->where("id = " . $game_user_info["id"])
                        ->update([
                            "area_type" => $area_type,
                            "stage_no" => $stage_no,
                            "stage_name" => $stage_name,
                            "number" => $number,
                            "zodiac" => $zodiac,
                        ]);
                        //有数据，则需要看用户绑定的数据是否锁定，未锁定就覆盖，锁定了不再改变
                        if($game_user_info["one_zodiac_one_number_lock"] == 0){
                            Db::name("game_user")
                            ->where("id = " . $game_user_info["id"])
                            ->update(["one_zodiac_one_number" => $one_zodiac_one_number, "one_zodiac_one_number_lock" => $one_zodiac_one_number_lock]);
                        }
                        if($game_user_info["three_zodiac_therr_number_lock"] == 0){
                            Db::name("game_user")
                            ->where("id = " . $game_user_info["id"])
                            ->update(["three_zodiac_therr_number" => $three_zodiac_therr_number, "three_zodiac_therr_number_lock" => $three_zodiac_therr_number_lock]);
                        }
                        if($game_user_info["six_zodiac_lock"] == 0){
                            Db::name("game_user")
                            ->where("id = " . $game_user_info["id"])
                            ->update(["six_zodiac" => $six_zodiac, "six_zodiac_lock" => $six_zodiac_lock]);
                        }
                        if($game_user_info["single_double_lock"] == 0){
                            Db::name("game_user")
                            ->where("id = " . $game_user_info["id"])
                            ->update(["single_double" => $single_double, "single_double_lock" => $single_double_lock]);
                        }
                        if($game_user_info["first_number_lock"] == 0){
                            Db::name("game_user")
                            ->where("id = " . $game_user_info["id"])
                            ->update(["first_number" => $first_number, "first_number_lock" => $first_number_lock]);
                        }
                        if($game_user_info["last_number_lock"] == 0){
                            Db::name("game_user")
                            ->where("id = " . $game_user_info["id"])
                            ->update(["last_number" => $last_number, "last_number_lock" => $last_number_lock]);
                        }
                        if($game_user_info["color_lock"] == 0){
                            Db::name("game_user")
                            ->where("id = " . $game_user_info["id"])
                            ->update(["color" => $color, "color_lock" => $color_lock]);
                        }
                        if($game_user_info["one_zodiac_lock"] == 0){
                            Db::name("game_user")
                            ->where("id = " . $game_user_info["id"])
                            ->update(["one_zodiac" => $one_zodiac, "one_zodiac_lock" => $one_zodiac_lock]);
                        }
                        if($game_user_info["six_number_lock"] == 0){
                            Db::name("game_user")
                            ->where("id = " . $game_user_info["id"])
                            ->update(["six_number" => $six_number, "six_number_lock" => $six_number_lock]);
                        }
                        if($game_user_info["one_number_lock"] == 0){
                            Db::name("game_user")
                            ->where("id = " . $game_user_info["id"])
                            ->update(["one_number" => $one_number, "one_number_lock" => $one_number_lock]);
                        }
                    }
                }
            }
        }

        $this->success("修改成功");
    }

    //发布/下架 活动
    public function releaseGame()
    {
        $id = $this->request->post("id", 0);

        if(!$id){
            $this->error("参数错误");
        }
        
        //查询活动信息
        $game_info = Db::name("game")
        ->where("id = " . $id)
        ->find();

        if($game_info["release"] == 1){
            $res = Db::name("game")
            ->where("id  = " . $id)
            ->update(["release" => 0]);
        }else{
            $res = Db::name("game")
            ->where("id  = " . $id)
            ->update(["release" => 1]);
        }

        if($res){
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }
    }

    //删除活动
    public function deleteGame()
    {
        $id = $this->request->post("id", 0);

        //查询数据放到回收站
        $info = Db::name("game")
        ->where("id = " . $id)
        ->find();

        if(!$info){
            $this->error("活动不存在");
        }

        $data = [];

        $data["area_type"] = $info["area_type"];
        $data["type"] = $info["type"];
        $data["stage_no"] = $info["stage_no"];
        $data["stage_name"] = $info["stage_name"];
        $data["number"] = $info["number"];
        $data["zodiac"] = $info["zodiac"];

        $data["one_zodiac_one_number"] = $info["one_zodiac_one_number"];
        $data["three_zodiac_therr_number"] = $info["three_zodiac_therr_number"];
        $data["six_zodiac"] = $info["six_zodiac"];
        $data["single_double"] = $info["single_double"];
        $data["first_number"] = $info["first_number"];
        $data["last_number"] = $info["last_number"];
        $data["color"] = $info["color"];
        $data["one_zodiac"] = $info["one_zodiac"];
        $data["six_number"] = $info["six_number"];
        $data["one_number"] = $info["one_number"];
        $data["createtime"] = $info["createtime"];
        $data["updatetime"] = $info["updatetime"];
        $data["deletetime"] = time();

        Db::name("game_del")
        ->insert($data);

        Db::name("game")
        ->where("id = " . $id)
        ->delete();

        Db::name("game_user")
        ->where("game_id = " . $id)
        ->delete();

        $this->success("删除成功");
    }

    //获取会员列表
    public function getUserListsAdmin()
    {
        //分页信息
        $searchText = $this->request->post("searchText","");
        $page = $this->request->post("page", 1);
        $limit = $this->request->post("limit", 10);

        $where = " 1 = 1 ";

        $searchText = (int) $searchText;

        if($searchText != ""){
            $where .= " AND (`id` LIKE '%" . $searchText . "%')";
        }

        $user_lists = Db::name("user")
        ->field("id, mobile_id, status, createtime, engine")
        ->where($where)
        ->page($page, $limit)
        ->select();

        if(!$user_lists){
            $this->success("暂无用户", []);
        }

        //数据处理
        foreach($user_lists as &$value){
            $value["id_name"] = sprintf("%04d", $value["id"]);
            if($value["status"] == "normal"){
                $value["status"] = 1; //1正常  0冻结
            }else{
                $value["status"] = 0;
            }
            
            $value["createtime"] = date("Y-m-d H:i:s", $value["createtime"]);
        }

        $this->success("操作成功", $user_lists);
        
    }

    //冻结，解冻用户
    public function updateUserStatus()
    {
        $uid = $this->request->post("uid", 0);

        if(!$uid){
            $this->error("参数错误");
        }
        
        //查询用户信息
        $user_info = Db::name("user")
        ->where("id = " . $uid)
        ->find();

        if($user_info["status"] == "normal"){
            $res = Db::name("user")
            ->where("id  = " . $uid)
            ->update(["status" => "hidden", "updatetime" => time()]);
        }else{
            $res = Db::name("user")
            ->where("id  = " . $uid)
            ->update(["status" => "normal", "updatetime" => time()]);
        }

        if($res){
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }

    }

    //修改用户主机状态
    public function updateUserEngine()
    {
        $uid = $this->request->post("uid", 0);

        if(!$uid){
            $this->error("参数错误");
        }
        
        //查询用户信息
        $user_info = Db::name("user")
        ->where("id = " . $uid)
        ->find();

        if($user_info["engine"] == 0){
            $res = Db::name("user")
            ->where("id  = " . $uid)
            ->update(["engine" => 1, "updatetime" => time()]);
        }else{
            $res = Db::name("user")
            ->where("id  = " . $uid)
            ->update(["engine" => 0, "updatetime" => time()]);
        }

        if($res){
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }

    }

    //获取当前的用户注册密码 后台登录密码
    public function getRegistCodeAdmin()
    {
        $lists = Db::name("regist_code")
        ->field("id, regist_code, name")
        ->where("status = 1")
        ->select();

        $this->success("操作成功", $lists); 
    }

    //修改注册码 或 管理端密码
    public function updateRegistCode()
    {
        $id = $this->request->post("id", 0);
        $regist_code = $this->request->post("regist_code", "");

        if(!$id || !$regist_code){
            $this->error("参数错误");
        }

        $res = Db::name("regist_code")
        ->where("id = " . $id)
        ->update(["regist_code" => $regist_code, "updatetime" => time()]);

        if($res){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }

    public function test()
    {
        print_r(
            json_encode(
                [
                    [
                        "zodiac" => "11"
                    ],
                    [
                        "zodiac" => "13"
                    ],
                    [
                        "zodiac" => "5"
                    ],
                    [
                        "zodiac" => "19"
                    ],
                    [
                        "zodiac" => "22"
                    ],
                    [
                        "zodiac" => "18"
                    ]
                ], JSON_UNESCAPED_UNICODE
            )
        );die;
    }
}
